package MyApp.View.well
{
	import MyApp.Model.Well;
	import MyApp.Model.WellGroup;
	import MyApp.Model.WellObsRecord;
	import MyApp.Model.WellPrdRecord;
	import MyApp.Model.WellXmlNode;
	import MyApp.Model.proxy.WellProxy;
	import MyApp.MyAppFacade;
	
	import components.well.AddObsRecord;
	import components.well.AddPrdRecord;
	import components.well.ObservationRecord;
	import components.well.ProductionRecord;
	import components.well.UpdateObsRecord;
	import components.well.UpdatePrdRecord;
	import components.well.WellContext;
	import components.well.WellWindow;
	
	import custom.pager.Pager;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import util.Event.ComponentCreatedEvent;
	import util.Event.ValueEvent;
	
	public class WellMediator extends Mediator
	{
		public static const NAME:String="WellMediator";
		public function WellMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			view.addEventListener(WellWindow.DO_SAVE,doSave);
			view.addEventListener(WellWindow.SELECT_WELL_CHANGE,selectWellChange);
			view.addEventListener(WellWindow.SELECT_TAB_CHANGE,selectTabChange);
			view.addEventListener(WellWindow.SELECT_TAB2_CHANGE,selectTab2Change);
			view.addEventListener(WellWindow.DO_ADD_OBSRECORD,doAddObsRecord);
			view.addEventListener(WellWindow.DO_UPDATE_OBSRECORD,doUpdateObsRecord);
			view.addEventListener(WellWindow.DO_DELETE_OBSRECORD,doDeleteObsRecord);
			view.addEventListener(WellWindow.ON_PRODUCTION_RECORD_CREATED,onPrdRecordCreated);
		}
		private function get view():WellWindow{
			return viewComponent as WellWindow;
		}
		
		protected function onPrdRecordCreated(e:ComponentCreatedEvent):void{
			facade.registerMediator(new ProductionRecordMediator(ProductionRecordMediator.NAME,view.prdRecord));
			this.sendNotification(ProductionRecord.DO_REFRESH);
		}
		protected function selectTabChange(e:ValueEvent):void{
			switch(e.vo){
				case 1:
					this.sendNotification(ObservationRecord.DO_REFRESH,new Pager());
					break;
			}
		}
		protected function selectTab2Change(e:ValueEvent):void{
			switch(e.vo){
				case 0:
					this.sendNotification(ProductionRecord.DO_REFRESH,new Pager());
					break;
			}
		}
		protected function doDeleteObsRecord(e:ValueEvent):void{
			var record:WellObsRecord=e.vo as WellObsRecord;
			this.sendNotification(WellWindow.DO_DELETE_OBSRECORD,{recordId:record.id,pager:view.obserRecord.record_list.mPager});
		}
		protected function doUpdateObsRecord(e:ValueEvent):void{
			var window:UpdateObsRecord=UpdateObsRecord(MyAppFacade.openPopUpWindow(UpdateObsRecord,UpdateObsRecordMediator,UpdateObsRecordMediator.NAME));
			window.record=e.vo as WellObsRecord;
			window.pager=view.obserRecord.record_list.mPager;
			window.well=view.well;
		}
		protected function doAddObsRecord(e:MouseEvent):void{
			var window:AddObsRecord = AddObsRecord(MyAppFacade.openPopUpWindow(AddObsRecord,AddObsRecordMediator,AddObsRecordMediator.NAME));
			window.well=view.well;
		}
		protected function selectWellChange(e:ValueEvent):void{
			var x:WellXmlNode=e.vo as WellXmlNode;
			if(x.type=="well"){
				var well:Well=this.getWell(x.id);
				view.well=well;
				WellContext.well=view.well;
				view.currentState="well";
			}else{
				var wg:WellGroup=this.getWellGroup(x.id);
				view.wellG=wg;
				WellContext.wellGroup=wg;
				view.currentState="group";
				this.sendNotification(ProductionRecord.DO_REFRESH);
			}
			view.nav.selectedIndex=0;
		}
		protected function doSave(e:MouseEvent):void{
			view.basicView.currentState="loading";
			var w:Well=view.well;
			this.sendNotification(WellWindow.DO_SAVE,w);
		}
		private function getWellXmlList(list:ArrayCollection):ArrayCollection{
			var xlist:ArrayCollection=new ArrayCollection();
			if(list){
				var node:WellXmlNode;
				var node2:WellXmlNode;
				var wg:WellGroup;
				var w:Well;
				for(var i:int=0; i<list.length; i++){
					wg=list.getItemAt(i) as WellGroup;
					node=new WellXmlNode();
					node.id=wg.wellGroupId;
					node.type="group";
					node.name=wg.wellGroupName;
					node.children=new ArrayCollection();
					if(wg.wellList){
						for(var j:int=0; j<wg.wellList.length; j++){
							w=wg.wellList.getItemAt(j) as Well;
							node2=new WellXmlNode();
							node2.id=w.id;
							node2.type="well";
							node2.name=w.name;
							node.children.addItem(node2);
						}
					}
					xlist.addItem(node);
				}
			}
			return xlist;
		}
		private function getWell(id:int):Well{
			var list:ArrayCollection=view.wellList;
			if(list){
				var wg:WellGroup;
				for(var i:int=0; i<list.length; i++){
					wg=list.getItemAt(i) as WellGroup;
					if(wg.wellList){
						for(var j:int=0; j<wg.wellList.length; j++){
							if(wg.wellList.getItemAt(j).id==id){
								return wg.wellList.getItemAt(j) as Well;
							}
						}
					}
				}
			}
			var well:Well=new Well();
			well.name="无";
			return well;
		}
		private function getWellGroup(id:int):WellGroup{
			var list:ArrayCollection=view.wellList;
			if(list){
				var wg:WellGroup;
				for(var i:int=0; i<list.length; i++){
					wg=list.getItemAt(i) as WellGroup;
					if(wg.wellGroupId==id){
						return wg;
					}
				}
			}
			var wg2:WellGroup=new WellGroup();
			wg2.wellGroupName="无";
			return wg2;
		}
		private function getFirstWell():Well{
			var list:ArrayCollection=view.wellList;
			if(list){
				var wg:WellGroup;
				for(var i:int=0; i<list.length; i++){
					wg=list.getItemAt(i) as WellGroup;
					if(wg.wellList){
						for(var j:int=0; j<wg.wellList.length; j++){
							if(wg.wellList.getItemAt(j)){
								return wg.wellList.getItemAt(j) as Well;
							}
						}
					}
				}
			}
			var well:Well=new Well();
			well.name="无";
			return well;
		}
		override public function listNotificationInterests():Array{
			return [WellProxy.WELL_LOAD_OK,WellProxy.SAVE_WELL_OK];
		}
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case WellProxy.WELL_LOAD_OK:
					var o:Object=notification.getBody();
					view.wellList=o as ArrayCollection;
					view.wellXmlList=this.getWellXmlList(o as ArrayCollection);
					view.well=this.getFirstWell();
					WellContext.well=view.well;
					break;
				case WellProxy.SAVE_WELL_OK:
					view.basicView.currentState="basic";
					break;
			}
		}
	}
}